package bawei.com.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.List;

import bawei.com.adapter.RcmdAdapter;

import bawei.com.bilibili.MyAppliction;
import bawei.com.bilibili.R;
import bawei.com.model.bean.RecommendBean;


/**
 * 类描述:
 * 作者：张宏达
 * 时间:2017/3/9 15:49
 * 邮箱:18310832074@163.com
 */

public class RecommendFragment extends Fragment {

    String url = "http://app.bilibili.com/x/feed/index?appkey=1d8b6e7d45233436&build=500001&idx=1489057149&mobi_app=android&network=wifi&platform=android&pull=true&style=2&ts=1489058253000&sign=e680da1865dc3924721bdb07a5972f66";

    List<RecommendBean.DataBean> dataBeen;
    private TextView mTextView1;
    private TextView mTextView2;
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    getData();
                    mSwipeRefreshLayout.setRefreshing(false);
                    break;
            }
        }
    };

    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.recommend_fragment, null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SopCastFragment.flag=false;
        //初始化控件的方法
        initView();
        //获取数据
        getData();

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.sendEmptyMessageDelayed(0, 2000);

            }
        });

    }

    private void initView() {
        mTextView1 = (TextView) getActivity().findViewById(R.id.rcmd_sive);
        mTextView2 = (TextView) getActivity().findViewById(R.id.rcmd_mend);
        mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.rcmd_rcv);
        mSwipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipRefreshlayout);

    }


    public void getData() {

        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
        //
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(final Response response) throws IOException {
                String json = response.body().string();

                Gson gson = new Gson();
                RecommendBean recommendBean = gson.fromJson(json, RecommendBean.class);
                dataBeen = recommendBean.getData();

                //主线程更新UI


                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // Toast.makeText(getActivity(), dataBeen.toString(), Toast.LENGTH_SHORT).show();

                        GridLayoutManager gdManager = new GridLayoutManager(getActivity(), 2);
                        mRecyclerView.setLayoutManager(gdManager);
                        //设置布局管理器 , 将布局设置成纵向
                        mRecyclerView.setAdapter(new RcmdAdapter(getActivity(), dataBeen));
                    }
                });
            }
        });

    }

}
